package com.estock.common.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	
	public static String PATTERN_SIMPLE_FORMAT = "yyyyMMdd";
	public static String PATTERN_DATETIME_FORMAT = "yyyy-MM-dd HH:mm";
	public static String PATTERN_TIME_FORMAT = "HH:mm";
	
	public static String parseString(Object value, String pattern) {
        String ret = "";
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        try {
            if (value instanceof Date) {
            	ret = format.format(value);
            } else if (value instanceof String) {
            	String val = (String) value;
                if (val.length() > 0) {
                    Date date = format.parse(val);
                    ret = format.format(date);
                }
            }
        } catch (Exception e) {
        	throw new RuntimeException("Error in parsing Date object to String.");
        }
        return ret;
    }
	
	public static boolean dayBefore(Date date1, Date date2) {
		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(date1);
		
		Calendar cal2 = Calendar.getInstance();
		cal2.setTime(date2);
	
		if (cal1.get(Calendar.YEAR) < cal2.get(Calendar.YEAR)) {
			return true;
		} else if (cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) &&
				cal1.get(Calendar.DAY_OF_YEAR) < cal2.get(Calendar.DAY_OF_YEAR)) {
			return true;
		} else {
			return false;
		}
	}
	
}
